package com.qms.repast.ui.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.orhanobut.logger.Logger;
import com.qms.repast.R;
import com.qms.repast.entity.MessageEvent;
import com.qms.repast.entity.resbean.CuisineBean;
import com.qms.repast.ui.adapter.OrderReviewAdapter;
import com.qms.repast.ui.base.BaseActivity;
import com.qms.repast.ui.presenter.OrderReviewPresenter;
import com.qms.repast.ui.view.IOrderReviewView;
import com.qms.repast.utils.AppUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author 泅渡者
 * @date 2018/10/11 0011
 * @describe: 订单预览界面-可操作
 */
public class OrderReviewActivity extends BaseActivity<OrderReviewPresenter> implements IOrderReviewView {
	@BindView(R.id.iv_back)
	ImageView ivBack;
	@BindView(R.id.tv_title)
	TextView tvTitle;
	@BindView(R.id.recyclerView)
	RecyclerView recyclerView;
	@BindView(R.id.tv_totalNum)
	TextView tvTotalNum;
	@BindView(R.id.tv_totalPrice)
	TextView tvTotalPrice;
	@BindView(R.id.tv_commit)
	TextView tvCommit;
	/**
	 * 店铺名称
	 */
	private String shopName;
	/**
	 * 店铺ID
	 */
	private String shopId;
	/**
	 * 用户ID
	 */
	private String userId;
	/**
	 * 菜品集合
	 */
	private List<CuisineBean> cuisineBeans;
	private OrderReviewAdapter reviewAdapter;

	private Intent mIntent;

	@Override
	protected int initLayout () {
		return R.layout.activity_order_review;
	}

	@Override
	protected void initPresenter (Intent intent) {
		mPresenter = new OrderReviewPresenter(this, this);
		shopName = intent.getStringExtra("shopName");
		shopId = intent.getStringExtra("shopId");
		userId = intent.getStringExtra("userId");
	}

	@Override
	protected void initView () {
		EventBus.getDefault().register(this);
		tvTitle.setText("订单预览");
		cuisineBeans = new ArrayList<>();
		reviewAdapter = new OrderReviewAdapter(AppUtils.getContext());
		recyclerView.setLayoutManager(new LinearLayoutManager(this));
		recyclerView.setAdapter(reviewAdapter);
		recyclerView.setNestedScrollingEnabled(false);
		reviewAdapter.setList(cuisineBeans, userId, shopId);

	}

	@Override
	protected void initData () {
		mPresenter.selectAllItem(userId, shopId);
	}

	@Override
	protected void onResume () {
		super.onResume();

	}

	@Subscribe
	public void onEventMainThread (MessageEvent messageEvent) {
		switch (messageEvent.getCode()) {
			//刷新菜品总数
			case 1211:
				mPresenter.selectItemCount(userId, shopId);
				break;
			default:
				break;
		}
	}

	@OnClick({R.id.iv_back, R.id.tv_commit})
	public void onViewClicked (View view) {
		switch (view.getId()) {
			case R.id.iv_back:
				finish();
				break;
			case R.id.ll_remarks:
				break;
			case R.id.tv_commit:
				mIntent = new Intent(this, OrderConfirmActivity.class);
				mIntent.putExtra("shopId",shopId);
				startActivity(mIntent);
				break;
			default:
				break;
		}
	}


	@Override
	protected void onDestroy () {
		super.onDestroy();
		if (EventBus.getDefault().isRegistered(this)) {
			EventBus.getDefault().unregister(this);
		}
	}

	@Override
	public void updateAllData (List<CuisineBean> cuisineOrderBeans) {
		Logger.json(new Gson().toJson(cuisineOrderBeans));
		reviewAdapter.setList(cuisineOrderBeans, userId, shopId);
	}

	@Override
	public void showItemCount (int size) {
		tvTotalNum.setText(size + "");
	}

}
